<?
        function getMatrices($xml_file) {
            global $matrizes;
            $objeto = new DomDocument();
            $objeto->load($xml_file);
            $filho = $objeto->getelementsbytagname("matrices");
            if ($filho->length > 0) {
                $temp = $filho->item(0)->getelementsbytagname("matrix");
                $j = 0;
                for($i = 0; $i < $temp->length; $i++) {
                    $conteudo = $temp->item($i)->getelementsbytagname("num_rows");
                    if ($conteudo->length > 1) {
                    $matrizes[$j] = $temp->item($i);
                    $j++;
                    }
                }
                return $matrizes;
            }
            else return null;
        }

        function getRows($indice) {
            global $matrizes, $r;
            $linhas = $matrizes[$indice]->getelementsbytagname("num_rows");
            $r[0] = $linhas->item(0)->nodeValue;
            $r[1] = $linhas->item(1)->nodeValue;
            return $r;
        }

        function getCols($indice) {
            global $matrizes, $c;
            $colunas = $matrizes[$indice]->getelementsbytagname("num_cols");
            $c[0] = $colunas->item(0)->nodeValue;
            $c[1] = $colunas->item(1)->nodeValue;
            return $c;
        }

        function getDesc($indice) {
            global $matrizes, $desc;
            $descricao = $matrizes[$indice]->getelementsbytagname("description");
            $desc = $descricao->item(0)->nodeValue;
            return $desc;
        }

        function getCom($indice) {
            global $matrizes, $com;
            $comentario = $matrizes[$indice]->getelementsbytagname("comments");
            if ($comentario->length > 0) {
                $com = $comentario->item(0)->nodeValue;
                return $com;
            }
            else return null;
        }

        function getRef($indice) {
            global $matrizes, $ref;
            $referencia = $matrizes[$indice]->getelementsbytagname("reference");
            if ($referencia->length > 0) {
                $ref = $referencia->item(0)->nodeValue;
                return $ref;
            }
            else return null;
        }

        function getMatrix($indice) {
            global $matrizes, $M;
            $r = getRows($indice);
            $linhas = $matrizes[$indice]->getelementsbytagname("row");
            for($i = 0; $i < $linhas->length; $i++) {
                $x = $linhas->item($i)->getelementsbytagname("x");
                if ($i < $r[0]) for($j = 0; $j < $x->length; $j++) {
                    $M[$i][$j] = $x->item($j)->nodeValue;
                }
                else for($j = 0; $j < $x->length; $j++) {
                    $M[$i][$j] = $x->item($j)->nodeValue;
                }
            }
            return $M;
        }


        function createMenu() {
            global $matrizes,$msg;
            for($i = 0; $i < sizeof($matrizes); $i++) {
                echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
                $temp = getRows($i); $temp1 = getCols($i);
                echo $msg->get('XML_EXIBE_MATRIZ_MSG06');
                echo "("."$temp[0]"."x"."$temp1[0]".") x ("."$temp[1]"."x"."$temp1[1]".")";
                echo "</u></a><p>";
            }
            return;
        }


?>

<? if (empty($xml_file) && empty($id_bib)) $id_bib = "bib_demo.xml"; 
    if ( (!isset($indice)) || ($indice == null) ) { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=multiplicacao.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib($xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getMatrices($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
	    } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
	    }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
               document.myform.Calcular.value = 'sim';
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=multiplicacao.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getMatrices($xml_file); ?>
        <? $r = getRows($indice);?>
        <? $c = getCols($indice);?>
        <? $rows = $r[0]; $rows2 = $r[1];?>
        <? $columns = $c[0]; $columns2 = $c[1];?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? $M = getMatrix($indice); ?>

        <? verificaBib($xml_file); ?>
        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MATRIZ_MSG03'); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        <p>
		<p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MATRIZ_MSG07'); ?>
        &nbsp</b>A<sub><? echo "$rows"."x"."$columns"; ?></sub><b>&nbsp; x &nbsp;</b>B<sub><? echo "$rows2"."x"."$columns2"; ?></sub></font>
		<p>
		<?
		//<table bordercolor="#000000" frame="border" rules="all" style="border:3px none">
		?>
		<table border="0">
        <tr><td>
        <table border="0">
		<? if ($rows > $rows2) { $tempr = $rows; } else { $tempr = $rows2; } ?>
		<? for ($i=0; $i < $rows; $i++) {	?>
            <tr>
            <? for ($j=0; $j<$columns; $j++) { ?>
	           <td align=center valign=center width=20> <? echo $M[$i][$j] ?> </td>
	           <input type="hidden" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo $M[$i][$j] ?>">
               <? } ?>
            </tr>
            <? } ?>
            </table>
            </td><td>
            <table><tr>
            <td width="20" align="middle" style="border:0px none">
            <b>X</b>
            </td>
            </tr></table>
            </td><td>
            <table>
            <? for ($i = 0; $i < $rows2; $i++) { ?>
            <tr>
	   	    <?	for ($j=0; $j<$columns2; $j++) { ?>
    		    <td align=center valign=center width=20> <? echo $M[$i+$rows][$j] ?> </td>
			    <input type="hidden" name="B[<? echo $i ?>][<? echo $j ?>]" value="<? echo $M[$i+$rows][$j] ?>">
            <? } /* end for j */?>
		    </tr>
		<? } //end for i ?>
        </table>
        </td></tr>
        </table>
        <p>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="rows" value="<? echo $rows ?>">
        <input type="hidden" name="rows2" value="<? echo $rows2 ?>">
        <input type="hidden" name="columns" value="<? echo $columns ?>">
        <input type="hidden" name="columns2" value="<? echo $columns2 ?>">
		<input type="hidden" name="navegacao" value="sim">
        <input type="hidden" name="Calcular" >
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG011'); ?> onClick="history.back()">
        </form>
<?	//NAVEGACAO ENTRE EXEMPLOS
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=multiplicacao.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo $msg->get('Outros_Exemplos')."<br>";
    $objeto = new DomDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("matrices");
	if ($filho->length > 0)
	{
		$temp = $filho->item(0)->getelementsbytagname("matrix");
		$j = 0;
		for($i = 0; $i < $temp->length; $i++)
		{
			$rows = $temp->item($i)->getelementsbytagname("num_rows");
			$cols = $temp->item($i)->getelementsbytagname("num_cols");
			if ($rows->length > 1)
			{
					echo ($j == 0) ? '' : ' | ';
					echo "<a class=\"bibliotecaexemplos\" href=\"javascript:criarLinks(".$j.")\" title=\"";
					echo "Matriz (".$rows->item(0)->nodeValue."x".$cols->item(0)->nodeValue.")x(";
					echo $rows->item(1)->nodeValue."x".$cols->item(1)->nodeValue.")\">";
					echo ($j == $indice)?"<b><u>":"<u>";
					echo ($j+1);
					echo ($j == $indice)?"</b></u>":"</u>";
					echo "</a>";
					$j++;
			}
		}
	}
} //else
?>
